{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1b7500f2",
   "metadata": {},
   "source": [
    "------\n",
    "* 记录人：莫慧敏\n",
    "* week14\n",
    "* 主要参考文档：\n",
    "> 1. [Python官方文档](https://python.org/doc)\n",
    "> 2. [博客-函数](https://www.cnblogs.com/xuzhichao/p/11570285.html)\n",
    "------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0e536672",
   "metadata": {},
   "source": [
    "# 上周回顾\n",
    "> 1. 函数的定义：def\n",
    "> 2. 函数名的定义规则-->变量名一样的规则\n",
    "> 3. 调用函数的方法\n",
    "> 4. 参数：\n",
    ">> 1. 行式参数（定义函数）\n",
    ">> 2. 实际参数（调用函数）\n",
    ">> 3. 有多个参数时：\n",
    ">>> 0. 关键字参数\n",
    ">>> 1. 位置参数（调用函数时）-->关键字参数要均放在末尾（多个不考虑顺序）\n",
    ">>> 2. 默认参数（定义函数时）-->默认参数均放在末尾（多个不考虑顺序）\n",
    "\n",
    "\n",
    "# 本周知识\n",
    "> 1. 函数的返回值\n",
    "> 2. 函数的参数类型\n",
    "> 3. 模块与函数的关系\n",
    "> 4.定义一个模块，然后调用模块\n",
    ">> 1. 调用模块\n",
    ">> 2. 调用模块中的函数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "117e36fe",
   "metadata": {},
   "source": [
    "## 函数的返回值\n",
    "> 1. 函数一定有返回值（无论是什么编程语言）\n",
    "> 2.在其他的编程语言中（如C语言，java等），返回值一定要写，不写会报错！；对于python（更高级的语言），返回值如果不写，给定默认值None\n",
    "> 3.返回值一般为函数体运行的结果（result）：也可以是自定义的函数返回值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "77c59944",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-06T07:09:05.894722Z",
     "start_time": "2021-12-06T07:09:05.877721Z"
    }
   },
   "outputs": [
    {
     "ename": "IndentationError",
     "evalue": "unexpected indent (<ipython-input-3-651b2221e83e>, line 5)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-3-651b2221e83e>\"\u001b[1;36m, line \u001b[1;32m5\u001b[0m\n\u001b[1;33m    return full_name\u001b[0m\n\u001b[1;37m    ^\u001b[0m\n\u001b[1;31mIndentationError\u001b[0m\u001b[1;31m:\u001b[0m unexpected indent\n"
     ]
    }
   ],
   "source": [
    "def get_formatted_name(first_name,last_name):\n",
    "    \"\"\"返回整洁的名字\"\"\"\n",
    "    full_name = first_name + '' + last_name\n",
    "#     print(full_name)\n",
    "     return full_name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "67e9e18b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-06T07:25:29.007953Z",
     "start_time": "2021-12-06T07:25:29.000952Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "LiHua\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "print(get_formatted_name(\"Li\",\"Hua\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f40bb292",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-06T07:27:44.559706Z",
     "start_time": "2021-12-06T07:27:44.549705Z"
    }
   },
   "outputs": [],
   "source": [
    "# 实践2\n",
    "def log_in(username,password):\n",
    "    \"\"\"验证登录信息\"\"\"\n",
    "    if username == \"Lihua\" and password == 123456:\n",
    "        return 1\n",
    "    else:\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "69505d15",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-06T07:28:11.271234Z",
     "start_time": "2021-12-06T07:28:11.205230Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "log_in(\"Lihua\",12345)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9e3423c5",
   "metadata": {},
   "source": [
    "# 函数的参数 \n",
    "## 函数输入的参数\n",
    "> 1. 函数可以输入复杂的参数：\n",
    ">> 1. 函数可以输入复杂的参数：\n",
    ">> 2. 函数参数为字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "9b7edd82",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-06T07:44:13.688281Z",
     "start_time": "2021-12-06T07:44:13.682280Z"
    }
   },
   "outputs": [],
   "source": [
    "def list_test(names):\n",
    "    \"\"\"测试输入参数为列表\"\"\"\n",
    "    print(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "13e50b96",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-06T07:45:04.322177Z",
     "start_time": "2021-12-06T07:45:04.307176Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Lihua', 'Xiaofeng', 'Ziqi']\n"
     ]
    }
   ],
   "source": [
    "list_test([\"Lihua\",\"Xiaofeng\",\"Ziqi\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "71550e60",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-06T07:50:32.471946Z",
     "start_time": "2021-12-06T07:50:32.463945Z"
    }
   },
   "outputs": [],
   "source": [
    "def dict_test(request_header):\n",
    "    \"\"\"测试输入参数为字典\"\"\"\n",
    "    print (request_header)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "316d6b33",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-06T07:50:35.202102Z",
     "start_time": "2021-12-06T07:50:35.188101Z"
    }
   },
   "outputs": [],
   "source": [
    "# https://httpbin.org/get\n",
    "httpbin_get = { \"args\": {}, \n",
    "  \"headers\": {\n",
    "    \"Accept\": \"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9\", \n",
    "    \"Accept-Encoding\": \"gzip, deflate, br\", \n",
    "    \"Accept-Language\": \"zh-CN,zh;q=0.9\", \n",
    "    \"Host\": \"httpbin.org\", \n",
    "    \"Sec-Ch-Ua\": \"\\\" Not A;Brand\\\";v=\\\"99\\\", \\\"Chromium\\\";v=\\\"96\\\", \\\"Google Chrome\\\";v=\\\"96\\\"\", \n",
    "    \"Sec-Ch-Ua-Mobile\": \"?0\", \n",
    "    \"Sec-Ch-Ua-Platform\": \"\\\"Windows\\\"\", \n",
    "    \"Sec-Fetch-Dest\": \"document\", \n",
    "    \"Sec-Fetch-Mode\": \"navigate\", \n",
    "    \"Sec-Fetch-Site\": \"none\", \n",
    "    \"Sec-Fetch-User\": \"?1\", \n",
    "    \"Upgrade-Insecure-Requests\": \"1\", \n",
    "    \"User-Agent\": \"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36\", \n",
    "    \"X-Amzn-Trace-Id\": \"Root=1-61adc04b-37b1c0091a25a9ff204b58e4\"\n",
    "  }, \n",
    "  \"origin\": \"61.242.54.175\", \n",
    "  \"url\": \"https://httpbin.org/get\"\n",
    "    \n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "94f321f7",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-06T07:50:39.541350Z",
     "start_time": "2021-12-06T07:50:39.526349Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9', 'Accept-Encoding': 'gzip, deflate, br', 'Accept-Language': 'zh-CN,zh;q=0.9', 'Host': 'httpbin.org', 'Sec-Ch-Ua': '\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"96\", \"Google Chrome\";v=\"96\"', 'Sec-Ch-Ua-Mobile': '?0', 'Sec-Ch-Ua-Platform': '\"Windows\"', 'Sec-Fetch-Dest': 'document', 'Sec-Fetch-Mode': 'navigate', 'Sec-Fetch-Site': 'none', 'Sec-Fetch-User': '?1', 'Upgrade-Insecure-Requests': '1', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36', 'X-Amzn-Trace-Id': 'Root=1-61adc04b-37b1c0091a25a9ff204b58e4'}\n"
     ]
    }
   ],
   "source": [
    "dict_test(httpbin_get['headers'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "de25cf1c",
   "metadata": {},
   "source": [
    "## 2.函数输出的返回值\n",
    "> 1. 返回值可以是字符串、数值、列表、字典等数据（单一值）\n",
    "> 2. 返回值是唯一的，不可以有两个（这是其他语言，如C语言，Java等）\n",
    "> 3. python可以输入多个返回值，结果会用一个元祖进行封装并返回。（本质不违背函数的定义）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "a577e1b6",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-06T07:34:41.705565Z",
     "start_time": "2021-12-06T07:34:41.700565Z"
    }
   },
   "outputs": [],
   "source": [
    "def test1_return():\n",
    "    \"\"\"测试返回值return\"\"\"\n",
    "    return \"你好\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "6a7632b5",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-06T07:35:16.233540Z",
     "start_time": "2021-12-06T07:35:16.208539Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'你好'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test1_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "4b546d1d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-06T08:13:39.790296Z",
     "start_time": "2021-12-06T08:13:39.776295Z"
    }
   },
   "outputs": [],
   "source": [
    "def test2_return():\n",
    "    \"\"\"测试返回值return\"\"\"\n",
    "    return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "31963c2b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-06T08:13:47.917761Z",
     "start_time": "2021-12-06T08:13:47.891759Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test2_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "d851d8aa",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-06T08:14:39.123690Z",
     "start_time": "2021-12-06T08:14:39.113689Z"
    }
   },
   "outputs": [],
   "source": [
    "def test3_return():\n",
    "    \"\"\"测试返回值return\"\"\"\n",
    "    return [1,2,3,4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "a2413478",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-06T08:14:40.894791Z",
     "start_time": "2021-12-06T08:14:40.869789Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test3_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "3a07e72b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-06T08:14:48.335216Z",
     "start_time": "2021-12-06T08:14:48.323216Z"
    }
   },
   "outputs": [],
   "source": [
    "def test4_return():\n",
    "    \"\"\"测试返回值return\"\"\"\n",
    "    return httpbin_get['headers']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "069aa690",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-06T08:14:50.482339Z",
     "start_time": "2021-12-06T08:14:50.463338Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',\n",
       " 'Accept-Encoding': 'gzip, deflate, br',\n",
       " 'Accept-Language': 'zh-CN,zh;q=0.9',\n",
       " 'Host': 'httpbin.org',\n",
       " 'Sec-Ch-Ua': '\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"96\", \"Google Chrome\";v=\"96\"',\n",
       " 'Sec-Ch-Ua-Mobile': '?0',\n",
       " 'Sec-Ch-Ua-Platform': '\"Windows\"',\n",
       " 'Sec-Fetch-Dest': 'document',\n",
       " 'Sec-Fetch-Mode': 'navigate',\n",
       " 'Sec-Fetch-Site': 'none',\n",
       " 'Sec-Fetch-User': '?1',\n",
       " 'Upgrade-Insecure-Requests': '1',\n",
       " 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36',\n",
       " 'X-Amzn-Trace-Id': 'Root=1-61adc04b-37b1c0091a25a9ff204b58e4'}"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test4_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "ef0c5191",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-06T08:15:01.370962Z",
     "start_time": "2021-12-06T08:15:01.358961Z"
    }
   },
   "outputs": [],
   "source": [
    "# 测试多个值\n",
    "def test5_return():\n",
    "    \"\"\"测试返回值return\"\"\"\n",
    "    return 1,2,\"Lihua\",[1,2,3],{\"name\":\"Alex\"}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "b9d27452",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-06T08:15:02.505027Z",
     "start_time": "2021-12-06T08:15:02.486026Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 2, 'Lihua', [1, 2, 3], {'name': 'Alex'})"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test5_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0480aee4",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "816756c3",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f286913d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2ea0078e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
